package cn.bdqn.pn.controller.user;

import cn.bdqn.pn.pojo.user.Resource;
import cn.bdqn.pn.pojo.user.Role;
import cn.bdqn.pn.pojo.user.RoleResource;
import cn.bdqn.pn.pojo.user.User;
import cn.bdqn.pn.service.user.RoleService;
import cn.bdqn.pn.service.user.role_ResourceService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    private RoleService roleService;


    @Autowired
    private role_ResourceService role_resourceService;

    @RequestMapping(value = "/selectRole")
    public String selectRole(Model model, @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize){
        Page<Role> rolePage=null;
        rolePage=roleService.selectRole(pageNo,pageSize);
        model.addAttribute("page",rolePage);
        /*Page<User> datas=userService.selectUserInfo(page,size);
        List<User> userList=datas.getContent();
        for (User user:userList
             ) {
            System.out.println(datas);
        }*/
        return "admin_Competence";
    }
    @RequestMapping(value = "/addRole")
    public String toAdd(Role role){
        return "addRole";
    }

    @RequestMapping("/add")
    public String add(Role role) {
        roleService.addRole(role);
        return "redirect:selectRole";
    }

    @RequestMapping(value = "/updateRole")
    public String update(Model model,Integer roleId) {
        Role role=roleService.findRoleById(roleId);
        model.addAttribute("role", role);
        return "updateRole";
    }

    @RequestMapping("/update")
    String updateRole(Role role){
        roleService.updateRole(role);
        return "redirect:selectRole";
    }
    @RequestMapping(value = "/deleteRole")
    String deleteRole(Integer roleId ){
        roleService.deleteRole(roleId);
        return "redirect:selectRole";
    }
    /*@RequestMapping(value = "selectAll")
    String selectAll(Model model){
        System.out.println("11111111111111111111111111");
        List<Role> list=new ArrayList<>();
        list=roleService.selectAll();
        model.addAttribute("allRole",list);
        return "Resource";
    }*/
    @RequestMapping(value = "/selectRoleDetail")
    String findRoleId(Model model, @Param("roleId") Integer roleId){
        List<RoleResource> roleList=role_resourceService.getAllRoleResourceByRoleId(roleId);
        model.addAttribute("roelDetail",roleList);
        System.out.println(JSON.toJSONString(roleList,true));
        return "admin_Competence::userinfo_refresh";
    }
    @RequestMapping(value = "/addrole_Resourc")
    public String toAdd(RoleResource role_resource){
        return "addrole_Resource";
    }

    @RequestMapping("/addRole_Resourc")
    public String add(Integer roleId,Integer resourceId) {
        System.out.println("=====================roleId="+roleId);
        System.out.println("=====================resourceId="+resourceId);
        RoleResource role_resource=new RoleResource();
        Role role=new Role();
        role.setRoleId(roleId);
        role_resource.setRole(role);
        Resource resource=new Resource();
        resource.setResourceid(resourceId);
        role_resource.setResource(resource);
        role_resourceService.addRoleResource(role_resource);
        return "redirect:selectRole";
    }
}
